import type { Instruction } from '@/types/AIResponse'
import Graphic from '@arcgis/core/Graphic'
import { ElMessage } from 'element-plus'
import { getTargetLayer } from './mapLayers'
import { showLayer } from './mapLayers'


// 初始化要素表单数据
export const initFeatureFormData = (layerType: string, aiAttributes: any) => {
  // 根据要素类型设置不同的表单字段
  switch (layerType) {
    case 'POINT':
      return {
        BuildingName: aiAttributes.name || '',
        BuildingType: aiAttributes.BuildingType || '',
        description: aiAttributes.description || '',
        createdAtTime: new Date().toISOString(),
      }
    case 'LINE':
      return {
        RoadName: aiAttributes.name || '',
        RoadType: aiAttributes.RoadType || '',
        description: aiAttributes.description || '',
        createdAtTime: new Date().toISOString(),
      }
    case 'POLYGON':
      return {
        AreaName: aiAttributes.name || '',
        ServiceType: aiAttributes.ServiceType || '',
        description: aiAttributes.description || '',
        createdAtTime: new Date().toISOString(),
      }
    default:
      return {}
  }
}

// 提交要素表单
export const submitFeatureForm = async (
  layerType: string,
  featureFormData: any,
  tempGraphic: Graphic | null,
  setFeatureFormVisible: (visible: boolean) => void,
  setCurrentInstruction: (instruction: Instruction | undefined) => void,
) => {
  try {
    // 验证必填字段
    let isValid = false
    console.log('featureFormData:', featureFormData)
    switch (layerType) {
      case 'POINT':
        isValid = !!featureFormData.BuildingName
        break
      case 'LINE':
        isValid = !!featureFormData.RoadName
        break
      case 'POLYGON':
        isValid = !!featureFormData.AreaName
        break
    }

    if (!isValid) {
      ElMessage.error('名称不能为空')
      return
    }

    // 执行最终添加操作
    const layer = getTargetLayer(layerType)
    const newFeature = new Graphic({
      geometry: tempGraphic!.geometry.clone(),
      attributes: featureFormData,
    })

    const result = await layer.applyEdits({
      addFeatures: [newFeature],
    })

    console.log('添加要素结果:', result)
    ElMessage.success('要素添加成功')

    // 关闭对话框并清理
    setFeatureFormVisible(false)
    showLayer.graphics.remove(tempGraphic!)
    setCurrentInstruction(undefined)
    return
  } catch (error) {
    console.error('添加要素失败:', error)
    ElMessage.error(`添加要素失败: ${error}`)
  }
}

// 字段名称翻译方法
export const getLabel = (field: string) => {
  const labels: { [key: string]: string } = {
    BuildingType: '建筑类型',
    BuildingName: '建筑名称',
    createdAtTime: '创建时间',
    description: '描述',
    RoadName: '道路名称',
    RoadType: '道路类型',
    AreaName: '区域名称',
    ServiceType: '服务类型',
    OBJECTID: '对象ID',
    // 周边查询结果相关字段
    poiList: '周边位置列表',
    centerLocation: '查询中心位置',
    radius: '查询半径',
    facilityType: '设施类型',
    name: '名称',
    address: '地址',
    distance: '距离',
    type: '类型',
    tel: '电话',
    location: '位置坐标',
    index: '序号',
  }
  return labels[field] || field
}